package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.api.domain.CatalogProperty;
import cn.wolfcode.shop.api.service.ICatalogPropertyService;
import cn.wolfcode.shop.api.service.ICatalogPropertyValueService;
import cn.wolfcode.shop.api.vo.JsonResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CatalogPropertyValueController {

    @Reference
    private ICatalogPropertyService catalogPropertyService;
    @Reference
    private ICatalogPropertyValueService catalogPropertyValueService;

    @RequestMapping("/catalogPropertyValue/get/{id}")
    public String get(@PathVariable("id") Long id,Model model){
        CatalogProperty catalogProperty = catalogPropertyService.get(id);
        model.addAttribute("catalogProperty",catalogProperty );
        return "catalog/property_value_list";
    }

    @RequestMapping("/catalogPropertyValue/delete/{id}")
    @ResponseBody
    public JsonResultVo delete(@PathVariable("id") Long id){
        JsonResultVo vo = new JsonResultVo(true,"操作成功!",null);
        try {
            catalogPropertyValueService.delete(id);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResultVo(false,"操作失败!",null);
        }
    }

    @RequestMapping("/catalogPropertyValue/add")
    @ResponseBody
    public JsonResultVo add(Long catalogPropertyId,String[] values){
        JsonResultVo vo = new JsonResultVo(true,"操作成功!",null);
        try {
            catalogPropertyValueService.save(catalogPropertyId,values);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResultVo(false,"操作失败!",null);
        }
    }
}
